/*
 * @author Brandon Wong 
 */
package com.apps.services;

import java.io.BufferedReader;
import java.util.regex.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;

import com.apps.datastore.dao.WorklistItemObject;
import com.apps.ubc.cc.model.FinalScheduleModel;
import com.apps.utils.WrapperUtils;


public class FinalExamScheduleService {

	// Matcher match = new Matcht
	private final static String regexFCourse = "(<DEPT>)(<COURSE>)\\s*\\.?\\s*(<SECTION>)?";

	public FinalExamScheduleService() {

	}

	private String getRegexString(String department, String course,
			String section) {

		String buffer = regexFCourse.toString();

		buffer = buffer.replace("<DEPT>", department);
		buffer = buffer.replace("<COURSE>", course);
		buffer = buffer.replace("<SECTION>", section);

		return buffer;
	}

	public List<FinalScheduleModel> getFinalSchedule(
			List<WorklistItemObject> list) {

		// The string is the section number. Note that you have to change the
		// section a little bit for it to work.
		// If the course number ends with a letter, concat the section number
		// with ".<SECTIONNUMBER>"
		// Else concat the section number with " .<SECTIONNUMBER>"; (SPACE and
		// then .<SECTIONNUMBER>);
		String time;
		String building;
		String department;
		String course;
		String section;
		String split;
		List<FinalScheduleModel> sch = new ArrayList<FinalScheduleModel>();

		try {
			BufferedReader br = WrapperUtils
					.getURLContentReader(WrapperUtils.FINAL_EXAMS_URL);

			String line = br.readLine();
			while (line!=null&&!line
					.contains("<div class=\"SidebarFeature\" id=\"AskMe\">")) {

				if (line.contains("<td>")) {
					line = WrapperUtils.stringInBwtn("<td>", "&nbsp;</td>",
							line);

					// list.
					Iterator<WorklistItemObject> itr = list.iterator();
					System.out.println("Line is " + line);
					while (itr.hasNext()) {
						WorklistItemObject model = itr.next();

						String name = getRegexString(model.getDeptId(),
								model.getCourseId(), model.getSectionId());

						// System.out.println("Line is: " + line);

						System.out.println("Name is: " + name);
						if (line.matches(name)) {

							department = model.getDeptId();
							course = model.getCourseId();
							section = model.getSectionId();

							// Found the right course and it's section. Now get
							// it's
							// time and value
							line = br.readLine();
							while (line!=null&&!line.contains("<td>")) {
								// just in case
								line = br.readLine();
							}
							// The first one is always time.
							// System.out.println("Time: " + line);
							time = WrapperUtils.stringInBwtn("<td>",
									"&nbsp;</td>", line);
							line = br.readLine();
							while (line != null && !line.contains("<td>")) {
								// just in case
								line = br.readLine();
							}

							String buffer = line;
							while (line != null && !line.contains("</td>")) {
								buffer = buffer.concat(line);
								line = br.readLine();
							}

							// Only gets building name
							building = WrapperUtils
									.stringInBwtn(
											"<a href=\"http://www.students.ubc.ca/classroomservices/buildings-and-classrooms/?code=",
											"\">", buffer);
							// Now gets room number
							int cursor=-1;
							if(buffer!=null){
							 cursor = buffer.indexOf("</a>");
							}
		
							if (buffer!=null&&cursor < buffer.length() && cursor != -1) {
								String[] buffering = buffer.split("</a>");
								if (buffering.length >= 1) {
									String room = buffering[1];
									room = room.trim();
									building = building.concat(" ")
											.concat(room);
								}

							}

							// There are two useless <td> tags. Loop through
							// them
							// and do nothing.

							while (line != null && !line.contains("<td>")) {
								// This gets the alpha split
								line = br.readLine();
							}
							split = WrapperUtils.stringInBwtn("<td>", "</td>",
									line);
							split = split.trim();
							line = br.readLine();
							while (line != null && !line.contains("<td>")) {
								line = br.readLine();
							}
							line = br.readLine();

							FinalScheduleModel finalmodel = new FinalScheduleModel(
									time, building, department, course,
									section, split);

							sch.add(finalmodel);

						}
					}
				}
				line = br.readLine();
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return sch;
	}

}
